package study;


import java.util.*;
/**
 * @author mayn
 */
public class NineGetCircle {

    public static void move(Node node) {
        if(node.state==1) {
            System.out.println("down "+node.num);
        } else {
            System.out.println("up "+node.num);
        }
    }

    public void play(Node[]node,ArrayList<Node> list,int n) {
        boolean deal=false;

        if(n==1) {
            if(node[n].state==1)
            {
                move(node[n]);
                node[n].state=0;
                list.remove(list.size()-1);
            }
            else
            {
                move(node[n]);
                node[n].state=1;
                list.add(node[n]);
            }
        }
        else {
            while(!deal)
            {
                if(node[n-1].state==1) {//前一环在上
                    if(list.get(list.size()-1).num==n-1)//前一环为栈顶
                    {
                        if(node[n].state==1)
                        {
                            move(node[n]);
                            node[n].state=0;
                            deal=true;
                            list.remove(list.size()-2);
                        }
                        else
                        {
                            move(node[n]);
                            node[n].state=1;
                            deal=true;
                            list.add(list.size()-1,node[n]);
                        }
                    }
                    else//前一环在上，但是前一环不是栈顶
                    {
                        int index=1;
                        for(int i=n-2;i>0;i--)//找到前一环之前的所有在上的环中最大的一个。
                        {
                            if(node[i].state==1) {
                                index=i;
                                break;
                            }
                        }
                        play(node,list,index);//将前一环之前的在上的最大的一环移走
                    }
                }
                //-------------------------------------------------------------------------
                else if(node[n-1].state==0) {//前一环不在上

                    play(node,list,n-1);
                }
            }
        }



    }

    public static long factorial(int n) throws Exception {
        if (n < 0) {
            throw new Exception("参数不能为负！");
        } else if (n == 1 || n == 0) {
            return n;
        } else {
            return factorial(n - 1) + factorial(n - 2);
        }
    }
    public int writeStars3(int length) {
        System.out.println("*");
        if(length<8){
            return 1;
        }else {
            return writeStars3(length - 1);
        }
    }
    public static void main (String[]args) throws Exception {
        System.out.println("请输入环数");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Node []node= new Node[n+1];
        for(int i=1;i<n+1;i++) {
            node[i]=new Node(i,1);
        }
        ArrayList<Node> list= new ArrayList<>();
        for(int j=n;j>0;j--) {
            list.add(node[j]);
        }
        NineGetCircle nineGetCircle= new NineGetCircle();
        for(int t=n;t>0;t--) {
            nineGetCircle.play(node, list,t);
        }
        System.out.println("d");
        System.out.println("阶乘结果是："+NineGetCircle.factorial(4));
        nineGetCircle.writeStars3(10);
    }
}

class Node{
    int num;
    int state;
    public Node(int num,int state) {
        this.num=num;
        this.state=state;
    }
}
